/**
 * Copyright (c) 2018 人人开源 All rights reserved.
 *
 * https://www.renren.io
 *
 * 版权所有，侵权必究！
 */

package com.jelly.redis;

import java.util.Collection;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.geo.Circle;
import org.springframework.data.geo.Distance;
import org.springframework.data.geo.GeoResults;
import org.springframework.data.geo.Metrics;
import org.springframework.data.geo.Point;
import org.springframework.data.redis.connection.RedisGeoCommands;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import com.jelly.query.CircleCenterQuery;
import com.jelly.vo.MarkersInfoVo;


/**
 * Redis工具类
 *
 * @author Mark sunlightcs@gmail.com
 */
@Component
public class RedisUtils {
	@Autowired
	private RedisTemplate<String, Object> redisTemplate;

	/** 默认过期时长为24小时，单位：秒 */
	public final static long DEFAULT_EXPIRE = 60 * 60 * 24L;
	/** 过期时长为1小时，单位：秒 */
	public final static long HOUR_ONE_EXPIRE = 60 * 60 * 1L;
	/** 过期时长为6小时，单位：秒 */
	public final static long HOUR_SIX_EXPIRE = 60 * 60 * 6L;
	/** 不设置过期时长 */
	public final static long NOT_EXPIRE = -1L;
	public final static String GEO_KEY = "geo:china:fish";

	public void set(String key, Object value, long expire) {
		redisTemplate.opsForValue().set(key, value);
		if (expire != NOT_EXPIRE) {
			expire(key, expire);
		}
	}

	public void set(String key, Object value) {
		set(key, value, DEFAULT_EXPIRE);
	}

	public Object get(String key, long expire) {
		Object value = redisTemplate.opsForValue().get(key);
		if (expire != NOT_EXPIRE) {
			expire(key, expire);
		}
		return value;
	}

	public Object get(String key) {
		return get(key, NOT_EXPIRE);
	}

	public void delete(String key) {
		redisTemplate.delete(key);
	}

	public void delete(Collection<String> keys) {
		redisTemplate.delete(keys);
	}

	public Object hGet(String key, String field) {
		return redisTemplate.opsForHash().get(key, field);
	}

	public Map<String, Object> hGetAll(String key) {
		HashOperations<String, String, Object> hashOperations = redisTemplate.opsForHash();
		return hashOperations.entries(key);
	}

	public void hMSet(String key, Map<String, Object> map) {
		hMSet(key, map, DEFAULT_EXPIRE);
	}

	public void hMSet(String key, Map<String, Object> map, long expire) {
		redisTemplate.opsForHash().putAll(key, map);

		if (expire != NOT_EXPIRE) {
			expire(key, expire);
		}
	}

	public void hSet(String key, String field, Object value) {
		hSet(key, field, value, DEFAULT_EXPIRE);
	}

	public void hSet(String key, String field, Object value, long expire) {
		redisTemplate.opsForHash().put(key, field, value);

		if (expire != NOT_EXPIRE) {
			expire(key, expire);
		}
	}

	public void expire(String key, long expire) {
		redisTemplate.expire(key, expire, TimeUnit.SECONDS);
	}

	public void hDel(String key, Object... fields) {
		redisTemplate.opsForHash().delete(key, fields);
	}

	public void leftPush(String key, Object value) {
		leftPush(key, value, DEFAULT_EXPIRE);
	}

	public void leftPush(String key, Object value, long expire) {
		redisTemplate.opsForList().leftPush(key, value);

		if (expire != NOT_EXPIRE) {
			expire(key, expire);
		}
	}

	public Object rightPop(String key) {
		return redisTemplate.opsForList().rightPop(key);
	}
	public void geoRemove(String member) {
		redisTemplate.opsForZSet().remove(GEO_KEY, member);
	}
	public void geoAdd(MarkersInfoVo markersInfo) {
		Double x = Double.valueOf(markersInfo.getLongitude());
		Double y =  Double.valueOf(markersInfo.getLatitude());
		redisTemplate.opsForGeo().add(GEO_KEY,
				new Point(x,y),
				markersInfo.getMember());
	}
	
	public GeoResults<RedisGeoCommands.GeoLocation<Object>> geoRadius(CircleCenterQuery query) {
		Double x = Double.valueOf(query.getLongitude());
		Double y =  Double.valueOf(query.getLatitude());
		Double radius = Double.valueOf(query.getRadius());
		//以选择的坐标为中心，查找数千米内的点位
		Circle circle = new Circle(new Point(x, y),new Distance(radius, Metrics.KILOMETERS));
		//通过给定坐标查找附近数千米的点位
		RedisGeoCommands.GeoRadiusCommandArgs args = RedisGeoCommands.GeoRadiusCommandArgs.newGeoRadiusArgs().includeDistance().includeCoordinates().sortAscending();
		GeoResults<RedisGeoCommands.GeoLocation<Object>> result = redisTemplate.opsForGeo().radius(GEO_KEY, circle, args);
		
		return result;
	}

}